package com.wman.sheep.common.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/14.
 */

public class GsonTool {
    // gson解析集合
    /**
     * 推荐方法
     *
     * @param jsonData
     * @param clazz
     * @return
     */
    public static <T> List<T> parseListFromJson(String jsonData, Class<T> clazz) {
        Type listType = new TypeToken<ArrayList<T>>() {}.getType();
        Gson gson = new Gson();
        List<T> tList = gson.fromJson(jsonData, listType);
        return tList;
    }
    public static <T> List<T> parseListFromJson(Object jsonData, Class<T> clazz) {
        Type listType = new TypeToken<List<T>>() {}.getType();
        Gson gson = new Gson();
        List<T> tList = gson.fromJson(gson.toJson(jsonData), listType);

        return tList;
    }

    public static <T> T parseOneFromJson(Object jsonData, Class<T> clazz) {
        Gson gson = new Gson();

        T user = gson.fromJson(gson.toJson(jsonData), clazz);

        return user;
    }
    public static <T> T parseOneFromJson(String jsonData, Class<T> clazz) {
        Gson gson = new Gson();

        T user = gson.fromJson(jsonData, clazz);

        return user;
    }
}
